'use strict';
module.exports = {
    /**
     * 检查租期满的展位订单
     * @url client/timedTask/pub/test 前端调用的url参数地址
     * data 请求参数
     * @param {String} params1  参数1
     */
    main: async (event) => {
        let { data = {}, userInfo, util, filterResponse, originalParam } = event;
        let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
        let { uid } = data;
        let res = {
            code: 0,
            msg: ""
        };
        // 业务逻辑开始-----------------------------------------------------------

        let { todayStart, todayEnd } = vk.pubfn.getCommonTime();
        res = await vk.baseDao.selects({
            dbName: "bidding-booths", // 主表名
            pageIndex: 1,
            pageSize: -1,
            getMain: true,
            whereJson: {
                status: 3,
                tenancy: _.lte(todayStart)//大于等于
            },
            foreignDB: [
                {
                    dbName: "bidding-contract", // 副表名
                    localKey: "booth_no", // 主表外键字段名
                    foreignKey: "booth_no", // 副表外键字段名
                    fieldJson: {
                        contract_id: "$_id",
                        order_no: true,
                        real_name: true
                    },
                    as: "contract",
                    limit: 1,
                },
                {
                    dbName: "bidding-order", // 副表名
                    localKey: "_id", // 主表外键字段名
                    foreignKey: "booth_id", // 副表外键字段名
                    whereJson: {
                        status: _.gt(1),
                        earnest_money_out_trade_no: _.neq(null)
                    },
                    foreignDB: [
                        {
                            dbName: "vk-pay-orders", // 副表名
                            localKey: "earnest_money_out_trade_no", // 主表外键字段名
                            foreignKey: "out_trade_no", // 副表外键字段名
                            as: "payorders",
                            fieldJson: {
                                _id: true,
                                total_fee: true,
                                user_id: true,
                                out_trade_no: true,
                                transaction_id: true,
                                transaction_id: true,
                                create_date: true
                            },
                            whereJson: {
                                status: 1,
                                iscreated: null,
                            },
                            limit: 1,
                        }],
                    as: "order",
                    limit: 1,
                },

            ],
            lastWhereJson: {
                "order.payorders": _.neq(null)
            }
        });
        console.log(res)

        let newArr = res.map(x => {

            return {
                booth_name: x.name,
                booth_no: x.booth_no,
                order_id: x.order._id,
                contract_no: x.order.contract_no,
                tenancy: x.tenancy,
                payorder_id: x.order.payorders._id,
                out_trade_no: x.order.payorders.out_trade_no,
                total_fee: x.order.payorders.total_fee,
                user_id: x.order.payorders.user_id,
                transaction_id: x.order.payorders.transaction_id,
                type: '押金',
                create_date: x.order.payorders.create_date
            }

        })




        for (let index = 0; index < newArr.length; index++) {
            const transaction = await vk.baseDao.startTransaction();
            try {
                const addModel = newArr[index];
                let num = await vk.baseDao.updateById({
                    db: transaction,
                    dbName: "vk-pay-orders", // 表名
                    id: addModel.payorder_id,
                    dataJson: { // 需要修改的数据
                        iscreated: 1,
                    }
                });
                let dbName = "biddings-refund-oreders";
                await vk.baseDao.add({
                    db: transaction,
                    dbName,
                    dataJson: {
                        ...addModel
                    }
                });
                await transaction.commit();

            } catch (err) {
                // 事务回滚
                await transaction.rollback();
                console.error(`transaction error`, err)
            }
        }
        // 业务逻辑结束-----------------------------------------------------------
        return res;
    }
}
